﻿//using AbstructFactory;
//using ADManage.api.Common;
//using Newtonsoft.Json;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using System.Web;
//using XF.Common;
//using XF.Meeting.Entity.CommonEntity;
//using XF.Meeting.Entity.CommonEntity.wx;
//using XF.Meeting.Entity.Query;
//using XF.Meeting.Entity.View;

//namespace XF.Meeting.Api.Common
//{
//    public class WeChatHelper
//    {
//            //ISettingDal settingDal = DalFactory.DalFactory.GetSettingDaL();
//            private HttpClientHelper requst = new HttpClientHelper();
//            public AbsFactoryTE abs = (AbsFactoryTE)AbsFactory.CreateInterface();
//            private string ACCESS_TOKEN { get; set; }
//            public WxConfig Wx { get; set; }
//            public WeChatHelper()
//            {
//            var service= abs.CreateBPMInterface("weixin", IPHelper.GetIPAddress);
//            Wx = service.GetWXCONFIG().Result;
//                ACCESS_TOKEN = GetAccessToken(Wx.Corpid, Wx.Secret);
//            }
//            public void SendTextMessage(Message message)
//            {
//            message.agentid = Convert.ToInt32(Wx.AgentId);
//                var response = requst.Post($"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={ACCESS_TOKEN}", JsonConvert.SerializeObject(message));
//            }

//            public string GetAccessToken(string corpid, string secret)
//            {

//                var response = requst.Get($"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}");
//                // Response.Write(response);
//                var aaa = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response);
//                return aaa.access_token;
//            }
//            public string GetUserId(string Code)
//            {

//                var response = requst.Get($"https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={ACCESS_TOKEN}&code={Code}");
//                var aaa = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response);
//                return aaa.UserId;
//            }
//            public string GetUserInfo(string UserId)
//            {

//                var response = requst.Get($"https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={ACCESS_TOKEN}&userid={UserId}");
//                //  var aaa = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response);
//                return response;
//            }
//            public string CreateChatId(WeChat_Chat enity)
//            {


//                var response = requst.Post($"https://qyapi.weixin.qq.com/cgi-bin/appchat/create?access_token={ACCESS_TOKEN}", JsonConvert.SerializeObject(new CreateChat
//                ()
//                {
//                    name = enity.name,
//                    owner = enity.owner,
//                    userlist = enity.userList,
//                    chatid = enity.chatid
//                }));

//                return enity.chatid;
//            }

//            public string SendMessageForGroup(WeChat_Chat_Msg entity)
//            {
//                var response = requst.Post($"https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token={ACCESS_TOKEN}", JsonConvert.SerializeObject(new ChatInfo()
//                {
//                    chatid = entity.ChatId,
//                    msgtype = entity.MsgType,
//                    safe = 0,
//                    text = new Text(entity.Msg)
//                }
//              ));
//                return response;
//            }
//            /// <summary>
//            /// 获取部门信息，
//            /// </summary>
//            /// <param name="id">为0查询全部</param>
//            /// <returns></returns>
//            public Task<Departments> GetDepartments(int id = 0)
//            {
//                var response = requst.Get($"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={ACCESS_TOKEN}&id={id}");
//                var depList = JsonConvert.DeserializeObject<Departments>(response);
//                return Task.FromResult(depList);
//            }
//            public Task<userList> GetUserList(int minId, int fetch_child)
//            {
//                var response = requst.Get($"https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={ACCESS_TOKEN}&department_id={minId}&fetch_child={fetch_child}");
//                var list = JsonConvert.DeserializeObject<userList>(response);
//                return Task.FromResult(list);
//            }
//            public void SendWxMssage(CRoomPadQuery query, bool isTen = false)
//            {
//            bool isZhouqi = false;
//                var dal = abs.CreateConferenceRoomInterface("pad", "");
//            int ID = 0;
//            if (query.type == 2)
//            {
//                 var cInfo = dal.getMeetIdBystrBOID(query.strBOID);
//                if (cInfo.CycleMeetType > 0)
//                {
//                    isZhouqi = true;
//                }
//                else {
//                    ID = cInfo.CInfoID;
//                }
//            }
//            else {
//                 ID = query.CInfoId;
//            }
//            if (!isZhouqi)
//            {
//                if (!dal.IsSendWX(ID, out string error))
//                {


//                    var res = dal.GetCInfoByID(ID).Result;

//                    var list = TableToList.ToList<MeetingInfoSingle>(res);
//                    var meetingInfoSingle = list[0];
//                    if (meetingInfoSingle.EndTime < DateTime.Now)
//                    {
//                        return;
//                    }
//                    meetingInfoSingle.date = Convert.ToDateTime(meetingInfoSingle.BeginTime).ToString("yyyy-MM-dd");
//                    meetingInfoSingle.Begin = Convert.ToDateTime(meetingInfoSingle.BeginTime).ToString("HH:mm");
//                    meetingInfoSingle.End = Convert.ToDateTime(meetingInfoSingle.EndTime).ToString("HH:mm");
//                    if (string.IsNullOrEmpty(meetingInfoSingle.CRooms))
//                    {
//                        meetingInfoSingle.CRooms = meetingInfoSingle.CRoom;
//                    }
//                    else
//                    {
//                        meetingInfoSingle.CRooms = meetingInfoSingle.CRoom + meetingInfoSingle.CRooms;
//                    }

//                    var msgList = dal.getMeetInfoAndUserPhone(ID, out string StrError);
//                    var phoneList = TableToList.ToList<meetIdS>(msgList.Tables[0]);
//                    var userlist = phoneList.Select(i => i.Account).ToList();
//                    var userr = GetUserString(userlist);
//                    var wx = new WeChatHelper();
//                    //XXX邀请您参加《XXXXX》会议，时间：2020-07-29 18:40~19:00，地点：XXXX，主持人：XXXX，请准时出席。
//                    var msg = new Message(new Text($"{meetingInfoSingle.CoordinatorName}邀请您参加《{meetingInfoSingle.ConferenceTheme}》会议，时间 {meetingInfoSingle.date}  {meetingInfoSingle.Begin}~{meetingInfoSingle.End}，地点：{meetingInfoSingle.CRooms}，主持人：{meetingInfoSingle.HostPersonName}，请准时出席。"), userr);
//                    //var msg = new Message(new Text($"请您参加于{meetingInfoSingle.date}，{meetingInfoSingle.Begin}到{meetingInfoSingle.End}，在（{meetingInfoSingle.CRooms}）举行的由（{meetingInfoSingle.HostPersonName}）主持的（{meetingInfoSingle.ConferenceTheme})主题会议，望准时参加!"), userr);
//                    wx.SendTextMessage(msg);
//                    if (isTen)
//                    {
//                        dal.updateWxState(ID, out string strError);
//                    }
//                }
//            }
             
       

//            }
//        public async void SendChnnelMsg(int id) {
//            var dal = abs.CreateConferenceRoomInterface("pad", "");
//            var res = await dal.GetCInfoByID(id);

//            var list = TableToList.ToList<MeetingInfoSingle>(res);
//            var meetingInfoSingle = list[0];
//            if (meetingInfoSingle.EndTime < DateTime.Now)
//            {
//                return;
//            }
//            meetingInfoSingle.date = Convert.ToDateTime(meetingInfoSingle.BeginTime).ToString("yyyy-MM-dd");
//            meetingInfoSingle.Begin = Convert.ToDateTime(meetingInfoSingle.BeginTime).ToString("HH:mm");
//            meetingInfoSingle.End = Convert.ToDateTime(meetingInfoSingle.EndTime).ToString("HH:mm");
//            if (string.IsNullOrEmpty(meetingInfoSingle.CRooms))
//            {
//                meetingInfoSingle.CRooms = meetingInfoSingle.CRoom;
//            }
//            else
//            {
//                meetingInfoSingle.CRooms = meetingInfoSingle.CRoom +meetingInfoSingle.CRooms;
//            }

//            var msgList = dal.getMeetInfoAndUserPhone(id, out string StrError);
//            var phoneList = TableToList.ToList<meetIdS>(msgList.Tables[0]);
//            var userlist = phoneList.Select(i => i.Account).ToList();
//            var userr = GetUserString(userlist);
//          //  var wx = new WeChatHelper();
//            //XXX邀请您参加《XXXXX》会议，时间：2020-07-29 18:40~19:00，地点：XXXX，主持人：XXXX，请准时出席。
//            var msg = new Message(new Text($"已取消:{meetingInfoSingle.CoordinatorName}邀请您参加《{meetingInfoSingle.ConferenceTheme}》会议，时间 {meetingInfoSingle.date}  {meetingInfoSingle.Begin}~{meetingInfoSingle.End}，地点：{meetingInfoSingle.CRooms}，主持人：{meetingInfoSingle.HostPersonName}，此会议已取消请知悉。"), userr);
//            //var msg = new Message(new Text($"请您参加于{meetingInfoSingle.date}，{meetingInfoSingle.Begin}到{meetingInfoSingle.End}，在（{meetingInfoSingle.CRooms}）举行的由（{meetingInfoSingle.HostPersonName}）主持的（{meetingInfoSingle.ConferenceTheme})主题会议，望准时参加!"), userr);
//            this.SendTextMessage(msg);
//        }
//        public void senNotAcceptMsg(string subject, string bO)
//        {
//            var msg = new Message(new Text($"{subject}"), "yulongge");
//            SendTextMessage(msg);
//        }


//        public static string GetUserString(List<string> userList)
//            {
//                StringBuilder sb = new StringBuilder();

//                foreach (var item in userList)
//                {
//                    //if (string.IsNullOrWhiteSpace(RedisCache.Hash_Get<string>("UserPhone", item)))
//                    //{
//                    //    continue;
//                    //}

//                    sb.Append( item);
//                    sb.Append("|");
//                }
//                sb.Remove(sb.Length - 1, 1);
//                return sb.ToString();
//            }

//        }
//        public class meetIdS
//        {
//            public int ID { get; set; }
//            public string Phone { get; set; }
//            public string Account { get; set; }
//        }
//    }
